//
//  WLMMCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-9.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "WLMMCheckViewController.h"
#import "WLMMCell.h"
@interface WLMMCheckViewController ()
{
    UITableView *_mainTable;
    NSArray *_checkNames;
    NSMutableArray *_floorNames;
    NSMutableArray *_floorNames1;
    NSMutableArray *checkTime;
    NSMutableArray *checkTime1;
    NSMutableArray *isNormal;
    NSMutableArray *isNormal1;
    NSMutableArray *location;
    NSMutableArray *location1;
    NSMutableArray *number;
    NSMutableArray *number1;
    NSArray *floorData;
    NSArray *floorData1;
}
@end

@implementation WLMMCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *checkURL = @"";
    if (self.delegate.checkAreaNumber == 17)
    {
        checkURL = resultOfCCTV;
        self.title = @"外立面幕墙自查巡检表";
    }
    else if (self.delegate.checkAreaNumber == 18)
    {
        checkURL = resultOfWaiLi2;
        _checkNames = @[@"有无破损",@"位置",@"数量",@"时间"];
        self.title = @"外立面幕墙特殊气候自查巡检表";
    }
    [self showMainView];
    _floorNames = [NSMutableArray array];
    _floorNames1 = [NSMutableArray array];
    checkTime = [NSMutableArray array];
    checkTime1 = [NSMutableArray array];
    isNormal = [NSMutableArray array];
    isNormal1 = [NSMutableArray array];
    location = [NSMutableArray array];
    location1 = [NSMutableArray array];
    number = [NSMutableArray array];
    number1 = [NSMutableArray array];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSURL *queryUrl = [NSURL URLWithString:checkURL];
    ASIFormDataRequest *obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:self.delegate.sendCheckTime forKey:@"checkTime"];
    [obtainFloor startAsynchronous];
    obtainFloor.delegate = self;
    [obtainFloor setDidFinishSelector:@selector(requestDone:)];
    [obtainFloor setDidFailSelector:@selector(requestFail)];
}
- (void)showMainView
{
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 90, whiteimage.size.width, whiteimage.size.height + 80)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, 40 + (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height + 40);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    UILabel *floorLb = [UILabel new];
    floorLb.text = @"材料";
    floorLb.font = [UIFont systemFontOfSize:30];
    CGSize mysz = [floorLb.text sizeWithFont:floorLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    floorLb.frame = CGRectMake(lineView.frame.origin.x + 20, (lineView.frame.origin.y - mysz.height)/2, mysz.width, mysz.height);
    [MyView addSubview:floorLb];
    
    for (int i = 0; i < _checkNames.count; i ++)
    {
        UILabel *floorLbNew = [UILabel new];
        floorLbNew.text = [_checkNames objectAtIndex:i];
        floorLbNew.font = [UIFont systemFontOfSize:20];
        mysz = [[NSString stringWithFormat:@"啊啊啊啊"] sizeWithFont:floorLbNew.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        floorLbNew.numberOfLines = 0;
        floorLbNew.textAlignment = 1;
        if (floorLbNew.text.length > 4)
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width, floorLb.frame.origin.y,mysz.width, mysz.height*2);
        }
        else
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width, floorLb.frame.origin.y + mysz.height/2,(MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count, mysz.height*(1 + mysz.width/(MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count));
        }
        [MyView addSubview:floorLbNew];
    }
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 2, lineImage.size.width-6, lineImage.size.height+20) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    [lineView addSubview:_mainTable];
    _mainTable.tag = 100;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return floorData.count;
    }
    else
    {
        return floorData1.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    WLMMCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[WLMMCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_floorNames.count > 0 && indexPath.section == 0)
    {
        cell.floorNameLb.text = [_floorNames objectAtIndex:indexPath.row];
        if (location.count > 0) {
            cell.locationLb.text = [location objectAtIndex:indexPath.row];
        }
        if (number.count > 0) {
            cell.numberLb.text = [number objectAtIndex:indexPath.row];
        }
        if (checkTime.count > 0) {
            cell.timeLb.text = [checkTime objectAtIndex:indexPath.row];
        }
        if (isNormal.count > 0 &&[[isNormal objectAtIndex:indexPath.row] isEqualToString:@"0"])
        {
            cell.isNormalLb.image = [UIImage imageNamed:@"正常"];
        }
        else
        {
            cell.isNormalLb.image = [UIImage imageNamed:@"重大问题"];
        }
    }
    if (_floorNames1.count > 0 && indexPath.section == 1) {
        cell.floorNameLb.text = [_floorNames1 objectAtIndex:indexPath.row];
        if (location1.count > 0) {
            cell.locationLb.text = [location1 objectAtIndex:indexPath.row];
        }
        if (number1.count > 0) {
            cell.numberLb.text = [number1 objectAtIndex:indexPath.row];
        }
        if (checkTime1.count > 1) {
            cell.timeLb.text = [checkTime1 objectAtIndex:indexPath.row];
        }
        if (isNormal1.count > 0 && [[isNormal1 objectAtIndex:indexPath.row] isEqualToString:@"0"]) {
            cell.isNormalLb.image = [UIImage imageNamed:@"正常"];
        }else{
            cell.isNormalLb.image = [UIImage imageNamed:@"重大问题"];
        }

    }
    return cell;
}
#pragma mark - 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:index];
    UILabel *label = [UILabel new];
    if (section == 0)
    {
        label.text = @"上午";
    }
    else
    {
        label.text = @"下午";
    }
    label.font = [UIFont systemFontOfSize:20];
    CGSize cgSz = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    label.frame = CGRectMake((cell.frame.size.width - cgSz.width)/2, (cell.frame.size.height - cgSz.height)/2, cell.frame.size.width, cell.frame.size.height);
    label.textAlignment = 1;
    label.backgroundColor = [UIColor lightGrayColor];
    return label;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    floorData = [queryDataArr objectForKey:@"floorData"];
    for (NSDictionary *dic in floorData)
    {
        [_floorNames addObject:[dic valueForKey:@"floorName"]];
        if ([dic valueForKey:@"date"])
        {
            [checkTime addObject:[dic valueForKey:@"date"]];
        }
        if ([[dic valueForKey:@"checkData"] count])
        {
            [isNormal addObject:[[[dic valueForKey:@"checkData"] objectAtIndex:0] valueForKey:@"result"]];
            [location addObject:[[[dic valueForKey:@"checkData"] objectAtIndex:1] valueForKey:@"result"]];
            [number addObject:[[[dic valueForKey:@"checkData"] objectAtIndex:2] valueForKey:@"result"]];
        }
    }
    
    NSDictionary *queryDataArr1 = [queryDic objectForKey:@"data1"];
    floorData1 = [queryDataArr1 objectForKey:@"floorData"];
    for (NSDictionary *dic in floorData1)
    {
        [_floorNames1 addObject:[dic valueForKey:@"floorName"]];
        if ([dic valueForKey:@"date"]) {
            [checkTime1 addObject:[dic valueForKey:@"date"]];
        }
        [isNormal1 addObject:[[[dic valueForKey:@"checkData"] objectAtIndex:0] valueForKey:@"result"]];
        [location1 addObject:[[[dic valueForKey:@"checkData"] objectAtIndex:1] valueForKey:@"result"]];
        [number1 addObject:[[[dic valueForKey:@"checkData"] objectAtIndex:2] valueForKey:@"result"]];
    }

    [_mainTable reloadData];
}
- (void)requestFail
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"数据请求失败！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [failAlert show];
}

#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
